﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Repositories.SearchExtensions;
using WebSite.ViewModels.ViewModelExtensions.DetailsViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.EditViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.IndexViewModelExtensions;

namespace WebSite.Controllers.ControllerExtensions
{
    public class ClienteControllerJolitexExtension : ControllerJolitexExtensionBase<Cliente>
    {
        protected override IIndexViewModelExtension DoGetIndexViewModelExtension(FormCollection formCollection, IRepository repository)
        {
            string codigoBuscaValue = formCollection["codigo"];
            string regiaoBuscaValue = formCollection["regiao"];
            return new ClienteJolitexIndexViewModel(codigoBuscaValue, regiaoBuscaValue);
        }

        public override IEditViewModelExtension EditViewModelExtension
        {
            get { return new ClienteJolitexEditViewModel(); }
        }

        public override IDetailsViewModelExtension DetailsViewModelExtension
        {
            get { return new ClienteJolitexDetailsViewModel(); }
        }

        public override ISearchExtension GetSearchExtension(FormCollection formCollection)
        {
            int? codigo = GetCodigo(formCollection);
            int? regiao = GetRegiao(formCollection);
            return new ClienteJolitexSearchExtension(codigo, regiao);
        }

        public override void RegisterIndexColumns(IIndexColumnsProvider indexColumnsProvider)
        {
            indexColumnsProvider.RegisterColumn("Código Jolitex", ClienteJolitexIndexColumns.Codigo);
            indexColumnsProvider.RegisterColumn("Região", ClienteJolitexIndexColumns.Regiao);
        }

        private static int? GetRegiao(FormCollection formCollection)
        {
            return GetIntValue("regiao", formCollection);
        }

        private static int? GetCodigo(FormCollection formCollection)
        {
            return GetIntValue("codigo", formCollection);
        }

        private static int? GetIntValue(string key, FormCollection formCollection)
        {
            string valueString = formCollection[key];
            int valueInt;
            return Int32.TryParse(valueString, out valueInt) ? (int?)valueInt : null;
        }

        protected override Dictionary<string, string> TryUpdateModel(Cliente model, FormCollection formCollection)
        {
            model.ClienteJolitex.Codigo = GetCodigo(formCollection);
            model.ClienteJolitex.Regiao = GetRegiao(formCollection);

            return new Dictionary<string, string>();
        }

        protected override Area Area { get { return Area.Clientes; } }

        protected override void SetModel(Cliente model)
        {
            if (model.ClienteJolitex == null)
            {
                model.SetClienteJolitex();
            }
        }
    }
}